package com.begye.smartloan.apptest.pingjia

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaPingJiaBinding
import com.begye.smartloan.apptest.fangkui.FangKuiActivity
import com.begye.smartloan.apptest.tool.addTouch
import com.data.analysislib.util.AppReviewUtil
import com.util.utillib.extend.openSystemBrowser

class PingJiaDialogFragment(private var parentActivity: AppCompatActivity) : DialogFragment() {

    private var _binding: DiaPingJiaBinding? = null
    private val binding get() = _binding!!

    private var _selectedStar = 0

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_ping_jia, container, false)
        _binding = DiaPingJiaBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        binding.diaPingJiaClose.setOnClickListener {
            dismiss()
        }

        binding.diaPingJiaStar1.setOnClickListener {
            _selectedStar = 1
            updateStarDisplay(_selectedStar)

            //
            addTouch(type = "ping_jia_dialog_star1_click")
        }
        binding.diaPingJiaStar2.setOnClickListener {
            _selectedStar = 2
            updateStarDisplay(_selectedStar)

            //
            addTouch(type = "ping_jia_dialog_star2_click")
        }
        binding.diaPingJiaStar3.setOnClickListener {
            _selectedStar = 3
            updateStarDisplay(_selectedStar)

            //
            addTouch(type = "ping_jia_dialog_star3_click")
        }
        binding.diaPingJiaStar4.setOnClickListener {
            _selectedStar = 4
            updateStarDisplay(_selectedStar)

            //
            addTouch(type = "ping_jia_dialog_star4_click")
        }
        binding.diaPingJiaStar5.setOnClickListener {
            _selectedStar = 5
            updateStarDisplay(_selectedStar)

            //
            addTouch(type = "ping_jia_dialog_star5_click")
        }

        binding.diaPingJiaNo.setOnClickListener {
            //
            addTouch(type = "ping_jia_dialog_no_click")

            dismiss()
        }

        binding.diaPingJiaYes.setOnClickListener {
            try {
                //
                addTouch(type = "ping_jia_dialog_rate_click")

                if (_selectedStar > 0) {
                    if (_selectedStar <= 3) {
                        val intent = Intent(parentActivity, FangKuiActivity::class.java).apply {
                            putExtra("star", _selectedStar.toString())
                        }
                        parentActivity.startActivity(intent)
                    } else {
                        AppReviewUtil.launchReviewFlow(parentActivity) {
                            if (!it) {
                                parentActivity.openSystemBrowser(MyApplication.storeLink)
                            }
                        }
                    }
                    dismiss()
                }
            } catch (e: Exception) {
//                Log.e("PingJiaDialogFragment", "Error in handling review action: ", e)
            }
        }
    }

    private fun updateStarDisplay(selected: Int) {
        val starResources = listOf(
            binding.diaPingJiaStar1,
            binding.diaPingJiaStar2,
            binding.diaPingJiaStar3,
            binding.diaPingJiaStar4,
            binding.diaPingJiaStar5
        )
        starResources.forEachIndexed { index, imageView ->
            val resId = if (index < selected) R.drawable.dres_132 else R.drawable.dres_133
            imageView.setImageResource(resId)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.CENTER
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        //
        addTouch(type = "ping_jia_dialog_show")
    }
}